<template>
  <header>
    <router-link tag="div" to="/" class="header-abs" v-show="showAbs">
      <span class="iconfont header-abs-icon">&#xe605;</span>
    </router-link>
    <div class="header-fixed" v-show="!showAbs" :style="opacityStyle">
      <router-link tag="div" to="/" class="header-fixed-bg">
        <span class="iconfont header-fixed-icon">&#xe605;</span>
      </router-link>
      景点详情
    </div>
  </header>
</template>
<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showAbs: true,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  // activated () {
  //   window.addEventListener('scroll', this.handleScroll, false)
  // },
  // deactivated () {
  //   window.removeEventListener('scroll', this.handleScroll, false)
  // },
  created () {
    window.addEventListener('scroll', this.handleScroll, false)
  },
  destoryed () {
    window.removeEventListener('scroll', this.handleScroll, false)
  },
  methods: {
    handleScroll () {
      const top = document.documentElement.scrollTop
      if (top > 45) {
        this.showAbs = false
        // >45 < 140时做一个透明度的变化
        let opc = top / 140
        opc = opc > 1 ? 1 : opc
        this.opacityStyle = {
          opacity: opc
        }
      } else {
        this.showAbs = true
      }
    }
  }
}
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl'
.header-abs
  position: absolute
  top: .4rem
  left: .4rem
  background: rgba(0, 0, 0, .8)
  width: 3.0rem
  line-height: 3.0rem
  border-radius: 50%
  text-align: center
.header-abs-icon
  color: #fff
.header-fixed
  height:$headerHeight
  color: #fff
  text-align: center
  background: $bgColor
  font-size: 1.8rem
  line-height: $headerHeight
  position: fixed
  top: 0
  left: 0
  right: 0
.header-fixed-bg
  position: absolute
  top: .4rem
  left: .4rem
  text-align: center
  width: 3.0rem
  line-height: 3.0rem
.header-fixed-icon
  color: #fff
</style>
